﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;

public partial class CardCenter_OnlinePurchaseCardStep2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ShoppingCat"] != null)
        {
            //购物车
            DataTable shoppingCat = (DataTable)Session["ShoppingCat"];
            this.DgCat.DataSource = shoppingCat.DefaultView;
            this.DgCat.DataBind();
        }
        else 
        {
            Response.Redirect("OnlinePurchaseCardStep1.aspx");
        }
       

        //绑定删除事件
        this.DgCat.DeleteCommand += new DataGridCommandEventHandler(DgCat_DeleteCommand);
    }

    //返回购卡页
    protected void ImgBtnReturn_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("OnlinePurchaseCardStep1.aspx");
    }

    //清空购物车
    protected void ImgBtnClear_Click(object sender, ImageClickEventArgs e)
    {
        DataTable shoppingCat = (DataTable)Session["ShoppingCat"];
        int k = shoppingCat.Rows.Count;
        DataRowCollection roc = shoppingCat.Rows;
        for (int i = 0; i < roc.Count;i++ )
        {
            roc[i].Delete();
        }
        shoppingCat.AcceptChanges();

        //重新绑定
        RebindToShoppingGrid(shoppingCat);
    }


    //进入在线支付
    protected void ImgBtnGoOnlinePay_Click(object sender, ImageClickEventArgs e)
    {
        DataTable shoppingCat = (DataTable)Session["ShoppingCat"];
        int totalMoney = 0;
        for (int i = 0; i < shoppingCat.Rows.Count;i++ )
        {
            string o_money = shoppingCat.Rows[i]["PerPrice"].ToString();
            string o_cardNum = shoppingCat.Rows[i]["CardNums"].ToString();
            if (string.IsNullOrEmpty(o_money))
            {
                o_money = "0";
            }

            if (string.IsNullOrEmpty(o_cardNum))
            {
                o_cardNum = "0";
            }

            int money = Convert.ToInt32(o_money);
            int cardNum = Convert.ToInt32(o_cardNum);

            totalMoney += money * cardNum;
        }

        Session["totalMoney"] = totalMoney;
        Response.Redirect("OnlinePurchaseCardStep3.aspx");
    }

    //更新购物车
    public void ImgBtnUpdate_Click(object sender, ImageClickEventArgs e) 
    {
       int k = this.DgCat.Items.Count;
       DataTable shoppingCat = (DataTable)Session["ShoppingCat"];
       for (int i = 0; i < k;i++ )
       {
           //取得ID
           string id = this.DgCat.Items[i].Cells[0].Text;
           //取得张数
           string cardNums = ((TextBox)this.DgCat.Items[i].Cells[4].Controls[1]).Text;
         
           DataRow[] row = shoppingCat.Select("pid ='" + id + "'");
           for (int j = 0; j < row.Length; j++)
           {
               DataRow editrow = row[j];
               editrow.BeginEdit();
               editrow["CardNums"] = cardNums;
               editrow.EndEdit();
               shoppingCat.AcceptChanges();
           }
       }
        //重新绑定
       RebindToShoppingGrid(shoppingCat);
    }


    //删除DataRow中的行
    public void DgCat_DeleteCommand(Object sender, DataGridCommandEventArgs e)
    {
        //取ID
        string id = e.Item.Cells[0].Text;
        //取张数
        string code = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
        int k = e.Item.ItemIndex;

        DataTable shoppingCat = (DataTable)Session["ShoppingCat"];
        string filter = "pid = " + id;
        DataRow[] row = shoppingCat.Select("pid ='" + id + "'");
        for (int i = 0; i < row.Length; i++)
        {
            shoppingCat.Rows.Remove(row[i]);
        }
        shoppingCat.AcceptChanges();
        //================
        //重新绑定
        RebindToShoppingGrid(shoppingCat);
    }


    //重新绑定数据
    private void RebindToShoppingGrid(DataTable shoppingCat)
    {
        Session["ShoppingCat"] = shoppingCat;
        this.DgCat.DataSource = ((DataTable)Session["ShoppingCat"]).DefaultView;
        this.DgCat.DataBind();
    }
}
